//
//	display welcome screen
//
(function (window){
	function MainScene (stage, contentManager, gameWidth, gameHeight) {
		this.MainSceneStage = stage;
		this.MainSceneContentManager = contentManager;
		this.gameHeight = gameHeight;
		this.gameWidth = gameWidth; 

		this.MainSceneContainer = new Container();
		this.MainSceneStage.addChild(this.MainSceneContainer);


		var instance = this;
		this.MainSceneContainer.mouseEnabled = true; 
		this.MainSceneContainer.onPress = function(event) {
			instance.handleKeyDown();
		}


		//variable flag
		//check if user's mouse over on BtnPlayImg
		this.flagBtnPlay = false;
		this.flagBtnPlayScale = 0;


		this.flagAbout = false;
		this.flagPayment = false;
		this.flagShare = false;
		this.flagAchieve = false;


		//Initial all of resources for this class
		this.mainImg 		= this.load(this.MainSceneContentManager.bg_main, 400, 240);
		this.btnPlayImg 	= this.load(this.MainSceneContentManager.btn_play, 400, 230, 0.8, 0.8);
		this.topBar 		= this.load(this.MainSceneContentManager.topbar_main, 400, 50, 0.6, 0.6);


		this.textCurrentLevel = this.createText("0", this.topBar.x, this.topBar.y - 13);
	
	

		// if this variable's value is true. that mean this class is acting.  
		this.flagAction = true;

	}



	MainScene.prototype.load = function (data, x, y, scaleX, scaleY) {
		var bitmap = new createjs.Bitmap(data);
		var width = bitmap.image.width;
		var height = bitmap.image.height;
		bitmap.x = x || 0;
		bitmap.y = y || 0;
		bitmap.regX = width/2;
		bitmap.regY = height/2;
		bitmap.scaleX = scaleX || 1;
		bitmap.scaleY = scaleY || 1;
		bitmap.width = width*bitmap.scaleX;
		bitmap.height = height*bitmap.scaleY;
		this.MainSceneContainer.addChild(bitmap);
		return bitmap;
	}



	MainScene.prototype.createText = function(text, x, y) {
		var text = new Text(text,'bold 30px Arial', '#FFF');
		text.alpha = 0.8;
		text.x = x;
		text.y = y;
		text.textAlign = 'center';
		this.MainSceneContainer.addChild(text);
		return text;
	}




	//Update function
	MainScene.prototype.tick = function() {
		if (this.flagAction) {
			this.checkMouse();
			this.update();
		} else {
			this.startRound.tick(); 
		}
	}



	MainScene.prototype.checkMouse = function() {
		var x = mousePos.x;
		var y = mousePos.y;

		this.setFalseFlag();
		
		if ( y >= this.btnPlayImg.y - this.btnPlayImg.height/2
			&& y <= (this.btnPlayImg.y + this.btnPlayImg.height/2) 
			&& (x > this.btnPlayImg.x - this.btnPlayImg.width/2) 
			&& x < (this.btnPlayImg.x + this.btnPlayImg.width/2)) 
		{
			this.flagBtnPlay = true;
		} 
		/*else if ((x > this.achieveButton12.x)
					 && x < (this.aboutButton12.x + this.aboutButton12.width) 
					 && y >= this.achieveButton12.y 
					 && y <= (this.achieveButton12.y + this.achieveButton12.height)) 
		{
			this.flagAchieve = true;
		}*/
	};


	/*
	* remove all of the flag
	*/
	MainScene.prototype.setFalseFlag = function() {
		if (this.flagBtnPlay) {this.flagBtnPlay=false; return;}
	}


	/**
	* perform update the state of button.
	*/
	MainScene.prototype.update = function() {
		var instance = this;

		if (this.flagBtnPlay && !this.flagBtnPlayScale) {
			this.flagBtnPlayScale = 2;
			createjs.Tween.get(this.btnPlayImg).to({scaleX: this.btnPlayImg.scaleX + 0.1,scaleY:this.btnPlayImg.scaleY + 0.1},200).call(function(){instance.flagBtnPlayScale = 1;});
		} else if (!this.flagBtnPlay && this.flagBtnPlayScale == 1) {
			this.flagBtnPlayScale = 2;
			createjs.Tween.get(this.btnPlayImg).to({scaleX: this.btnPlayImg.scaleX - 0.1,scaleY:this.btnPlayImg.scaleY - 0.1},100).call(function(){instance.flagBtnPlayScale = 0;});
		} 


		
	};


	/*
	* do an event depending on certain button that user press on 
	*/
	MainScene.prototype.handleKeyDown = function() {
		if (this.flagBtnPlay) {

			this.MainSceneStage.removeAllChildren();

			this.startRound = new  SecondRound(this.MainSceneStage, this.MainSceneContentManager, this.gameWidth, this.gameHeight);
			this.MainSceneStage.addChild(mouseCoordinator);

			this.flagAction = false;

			this.MainSceneContentManager.sStart.play();
			this.MainSceneContentManager.sStartBegin.play();
		}
	}

	window.MainScene = MainScene;
}(window));